Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
When you use ReDim to change the size of a dynamic array, the default is for all data in the array to be lost. This means that all elements in the newly sized array will be initialized to zero (numeric arrays), an empty string (string arrays), the Empty value (Variant arrays), or Nothing (object arrays). If you want to change the array’s size while preserving its data, you can do so (with some limitations) by using the Preserve keyword. Clearly this is applicable only the second and subsequent times the array’s size is changed with ReDim, otherwise there will be no data in it to save. Here’s how to use Preserve:
    ReDim Preserve MyArray(100)
There are some limitations on using Preserve: • If you increase the number of elements in the array, data in the existing elements will be preserved, and the new elements will be initialized by the usual rules. • With a multidimensional array, you can use Preserve only to change the last dimension of the array. Thus, after ReDim X(5, 5) you could execute ReDim Preserve X(5,10) but not ReDim Preserve X(10, 5). • You cannot use Preserve when you are changing the number of array dimensions. There’s a lot more to arrays, including multidimensional arrays, but at least you know enough to get started using them. I’ll deal with the other array topics as we encounter them throughout the book. Because the size of a dynamic array can change, it is useful to be able to determine its upper and lower bounds—that is, the highest and lowest index values that can be used. The LBound and UBound functions let you do this. The syntax is as follows:
    UBound(arrayname[, dimension])
    LBound(arrayname[, dimension])
The dimension argument is used for multidimensional arrays only, and it specifies the dimension whose upper or lower index is to be returned by the function. If the argument is omitted, one is assumed. Here are some examples, using an array that was resized as follows:
    Redim Data(10, 5 to 20)
    UBound(Data) returns 10
    LBound(Data, 2) returns 5
    LBound(Data, 1) returns 0
The UBound and LBound functions can be used with static arrays as well as dynamic arrays. #### Constants You can use two types of constants in a Visual Basic program: literal and symbolic. A literal constant is nothing more than a number or string typed directly into your source code. In the following lines of code,
    Dim MyString As String, MyNumber As Integer
    MyString = “New York”
    MyNumber = 123
“New York” and 123 are literal constants. You type them in while you are editing your source code, and they (of course) do not change during program execution—they are “constant.” Much more useful is the so-called symbolic constant. It has a name and a data type, just like a variable, and generally follows the same rules. Here is the difference: A symbolic constant is assigned a value when it is declared, and this value cannot change during program execution. To create a symbolic constant, use the Const keyword in the declaration statement:
    Const MAXIMUM As Integer = 100
This statement creates a constant named MAXIMUM with the value 100. You could then use the name MAXIMUM anywhere in the program where you could use a literal constant 100, and the result would be the same. Note that the As part of the Const declaration is optional. If you do not specify a data type, Visual Basic will automatically use the type most appropriate for the specified value. For example, the declaration
    Const RATIO = 15.12
will result in RATIO being assigned type Single. What is the purpose of symbolic constants? Why not just type in literal constants wherever they are needed? The major advantage is being able to change the value of a symbolic constant throughout the program simply by editing its declaration statement. Another advantage is that the constant’s name can help in making the program easier to read. You may have noticed that in the previous two examples, I used all uppercase for the con-stant names. This is not required by Visual Basic. In fact, the rules for naming constants are the same as for naming variables. Using all uppercase for constant names, while identifying variable names with a combination of upper- and lowercase makes it easy for me to distinguish constants from variables in my own code—a distinction that would not otherwise be apparent. This wraps up our introduction to Visual Basic variables and constants. You now have the fundamentals under your belt, although we have some ground yet to cover in regard to Visual Basic data storage. In particular, arrays and the Type statement are indispensable tools. As mentioned earlier, I will cover these and other topics later in the book as the need arises. TIP: Commenting Your Code Using comments liberally in your Basic code is a good idea. Visual Basic ignores comments; their only purpose is to provide anyone reading the code with reminders or explanations of what’s going on. Some programmers don’t use comments, or they use them sparingly—and they are usually sorry later. Aspects of your code design and program structure that seem perfectly clear during the writing process may appear less than crystalline a month or a year later when you need to modify the code. To insert a comment, type an apostrophe anywhere in your code, except within a literal string (that is, inside double quotation marks). Anything from the apostrophe to the end of the line is treated as a comment and ignored by the Visual Basic compiler. #### Enums An enumeration, identified by the Visual Basic keyword Enum, is used to associate sequential integer values with names. These names then serve as constants within the program, but with certain advantages over regular constants declared with the Const keyword. To declare an enumeration, use the Enum…End Enum statement:
    Public Enum OrderStatus
         pgaBackordered
         pgaInProgress
         pgaShipped
    End Enum
This code creates an enumeration named OrderStatus with three members. The Public keyword is optional; all enumerations are public by default. You can declare an enumeration as Private in which case its members will be visible only in the module where it is declared. Note that enumerations can be declared only in module-level code within a class or code module. When you do not assign specific values to the members of an enumeration, they are assigned integer values starting at zero. Therefore, pgaBackordered = 0, pgaInProgress = 1, and so on. To start at a value other than zero, you would assign the desired starting value to the first member:
    Public Enum OrderStatus
       PgaBackordered = 1
       pgaInProgress
       pgaShipped
    End Enum
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.